package com.fileshare.web.service.impl;

import com.fileshare.web.dao.DownloadRepository;
import com.fileshare.web.entity.DownloadLog;
import com.fileshare.web.entity.FileResource;
import com.fileshare.web.entity.User;
import com.fileshare.web.service.DownloadService;
import com.fileshare.web.utils.SystemUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.fileshare.web.constant.EntityConstant.FILE_RESOURCE;

@Service
public class DownloadServiceImpl implements DownloadService {
    @Resource
    private DownloadRepository downloadRepository;

    @Override
    public void saveLog(List<FileResource> resources) {
        User user = SystemUtil.getLoginUser();
        if (Objects.isNull(user)) {
            return;
        }
        Date now = new Date();
        for (FileResource resource : resources) {
            DownloadLog log = new DownloadLog();
            log.setCreateBy(user.getUserSeq());
            log.setUpdateBy(user.getUserSeq());
            log.setReferenceKey(FILE_RESOURCE);
            log.setReferenceValue(resource.getFileSeq());
            log.setCreateAt(now);
            log.setUpdateAt(now);
            downloadRepository.save(log);
        }
    }
}
